package com.topc.problem.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.core.sevice.RedisService;
import com.topc.problem.mapper.ProblemUserMidMapper;
import com.topc.problem.model.dto.QueryProblemUserMidDto;
import com.topc.problem.model.po.JudgeInfo;
import com.topc.problem.model.po.ProblemCommitStatusCollect;
import com.topc.problem.model.po.ProblemContentCommit;
import com.topc.problem.model.po.ProblemUserMid;
import com.topc.problem.model.vo.ProblemCommitShowVo;
import com.topc.problem.model.vo.ProblemListShowVo;
import com.topc.problem.repository.ProblemCommitStatusCollectRepository;
import com.topc.problem.service.IProblemContentCommitService;
import com.topc.problem.service.IProblemUserMidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemUserMidServiceImpl extends ServiceImpl<ProblemUserMidMapper, ProblemUserMid> implements IProblemUserMidService{

    @Autowired
    private ProblemCommitStatusCollectRepository problemCommitStatusCollectRepository;

    @Autowired
    private IProblemContentCommitService problemContentCommitService;

    @Autowired
    RedisService redisService;


    @Override
    public PageResult<ProblemUserMid> searchByPage(int pageSize, int pageCurrent, QueryProblemUserMidDto queryProblemUserMidDto) {
        LambdaQueryWrapper<ProblemUserMid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getProblemId()),ProblemUserMid::getProblemId,queryProblemUserMidDto.getProblemId())
                .eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getUserNumber()),ProblemUserMid::getUserNumber,queryProblemUserMidDto.getUserNumber())
                .eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getCompetitionId()),ProblemUserMid::getCompetitionId,queryProblemUserMidDto.getCompetitionId());
        return PageUtils.pageToResult(this.getBaseMapper().selectPage(PageUtils.toPageBuild(pageSize,pageCurrent),lambdaQueryWrapper));
    }

    @Override
    public boolean insertCommit(ProblemUserMid problemUserMid) {
        ProblemCommitStatusCollect qcsc = new ProblemCommitStatusCollect();
        qcsc.setCommitStatus(problemUserMid.getCommitStatus());
        ProblemCommitStatusCollect saveOne = problemCommitStatusCollectRepository.save(qcsc);
        if (ObjectUtil.isEmpty(saveOne)) return false;
        problemUserMid.setConditionMid(saveOne.getId());
        ProblemContentCommit entity = new ProblemContentCommit();
        entity.setContent(problemUserMid.getContent()).setCodeType(problemUserMid.getCodeType());
        boolean save = problemContentCommitService.save(entity);
        if (!save){
            problemCommitStatusCollectRepository.deleteById(saveOne.getId());
            return false;
        }
        problemUserMid.setContentId(entity.getId());
        return this.save(problemUserMid);
    }

    @Override
    public List<ProblemCommitShowVo> getProblemCommitList(String userNumber, Long problemId, Long competitionId) {
        LambdaQueryWrapper<ProblemUserMid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProblemUserMid::getUserNumber,userNumber)
                .eq(ProblemUserMid::getCompetitionId,competitionId)
                .eq(ProblemUserMid::getProblemId,problemId)
                .last("limit 10");//语句末尾添加限制数量
        List<ProblemUserMid> problemUserMidList = this.list(lambdaQueryWrapper);
        List<ProblemCommitShowVo> problemCommitShowVoList = new ArrayList<>();
        for (ProblemUserMid problemUserMid : problemUserMidList){
            ProblemCommitShowVo problemCommitShowVo = new ProblemCommitShowVo();
            String conditionMid = problemUserMid.getConditionMid();
            JudgeInfo judgeInfo = JSONUtil.toBean(conditionMid, JudgeInfo.class);
            problemCommitShowVo.setJudgeInfo(judgeInfo);
            problemCommitShowVo.setContentId(problemUserMid.getContentId());
            problemCommitShowVoList.add(problemCommitShowVo);
        }
        return problemCommitShowVoList;
    }

}
